{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# ATL03_viewer.ipynb\n",
    "# view ATL03 data in a variety of plotting formats\n",
    "#"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# import libraries\n",
    "# utility modules\n",
    "import glob\n",
    "import os\n",
    "import sys\n",
    "import re\n",
    "\n",
    "# the usual suspects:\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# specialty modules\n",
    "import h5py\n",
    "import pyproj\n",
    "\n",
    "# run matplotlib in 'widget' mode\n",
    "%matplotlib widget\n",
    "%load_ext autoreload\n",
    "%autoreload 2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# get HDF5_ATL03 reader\n",
    "sys.path.insert(1, '/home/jovyan/FirnAndMelt/scripts')\n",
    "from read_HDF5_ATL03 import read_HDF5_ATL03\n",
    "from get_ATL03_x_atc import get_ATL03_x_atc\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# define data directory\n",
    "data_root='/home/jovyan/shared/FirnAndMelt/ATL03/Greenland/'\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# read some data:\n",
    "#rgt=\"0300\"\n",
    "#cycle=\"01\"\n",
    "# read the IS2 data with ATL03 reader:\n",
    "#ATL03_file=glob.glob(os.path.join(data_root, f'*{rgt}{cycle}*.h5'))[0]\n",
    "#IS2_atl03_mds, IS2_atl03_attrs, IS2_atl03_beams =read_HDF5_ATL03(ATL03_file)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# add x_atc to the ATL03 data structure (this function adds to the LS2_ATL03_mds dictionary)\n",
    "#get_ATL03_x_atc(IS2_atl03_mds, IS2_atl03_attrs, IS2_atl03_beams)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# plot ground track on global map\n",
    "def track_plot(lat,lon,trk):\n",
    "    # plot satellite ground track on map using lat/lon coordinates for track number, trk\n",
    "    coastline_data = np.loadtxt('Coastline.txt',skiprows=1)\n",
    "    w, h = plt.figaspect(0.5)\n",
    "    fig = plt.figure(figsize=(w,h))\n",
    "    ax = fig.gca()\n",
    "    ##\n",
    "    fgcolor='black'\n",
    "    bgcolor='white'\n",
    "    fig.suptitle(\"Track %d\" %trk,fontsize=16,color=fgcolor)\n",
    "    plt.plot(coastline_data[:,0],coastline_data[:,1],'g')\n",
    "    ax.set_xlabel('Longitude (deg)',fontsize=14,color=fgcolor)\n",
    "    ax.set_ylabel('Latitude (deg)',fontsize=14,color=fgcolor)\n",
    "    ax.axes.tick_params(color=fgcolor,labelcolor=fgcolor)\n",
    "    ax.patch.set_facecolor(bgcolor)\n",
    "    plt.xlim(-180,180)\n",
    "    plt.ylim(-90,90)\n",
    "    plt.yticks([-90,-80,-70,-60,-50,-40,-30,-20,-10,0,10,20,30,40,50,60,70,80,90])\n",
    "    plt.xticks([-180,-150,-120,-90,-60,-30,0,30,60,90,120,150,180])\n",
    "    ##\n",
    "    for i1 in range(0,len(lat)):\n",
    "        plt.plot(lon[i1],lat[i1],'r.',markersize=1)\n",
    "    ax.grid(True)\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "917d1fa7087d46cd9e0bfd9f79a84c30",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Canvas(toolbar=Toolbar(toolitems=[('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous …"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# read some data:\n",
    "rgt=\"0483\"\n",
    "cycle=\"01\"\n",
    "# read the IS2 data with ATL03 reader:\n",
    "ATL03_file=glob.glob(os.path.join(data_root, f'*{rgt}{cycle}*.h5'))[0]\n",
    "IS2_atl03_mds, IS2_atl03_attrs, IS2_atl03_beams =read_HDF5_ATL03(ATL03_file)\n",
    "\n",
    "# add x_atc to the ATL03 data structure (this function adds to the LS2_ATL03_mds dictionary)\n",
    "get_ATL03_x_atc(IS2_atl03_mds, IS2_atl03_attrs, IS2_atl03_beams)\n",
    "\n",
    "#-- select the 2l beam from ATL03\n",
    "D3 = IS2_atl03_mds['gt2l']\n",
    "#print(D3.keys())\n",
    "#print(D3['heights'])\n",
    "\n",
    "#-- create scatter plot of photon data (e.g., photon elevation vs x_atc)\n",
    "fig=plt.figure(figsize=(6,4))\n",
    "ax = fig.add_subplot(111)\n",
    "ax.plot(D3['heights']['x_atc'], D3['heights']['h_ph'],'k.',markersize=0.25, label='all photons')\n",
    "LMH=D3['heights']['signal_conf_ph'][:,3] >= 2\n",
    "ax.plot(D3['heights']['x_atc'][LMH], D3['heights']['h_ph'][LMH],'g.',markersize=0.5, label='flagged photons')\n",
    "h_leg=ax.legend()\n",
    "\n",
    "ax.set_xlabel('x_atc, m')\n",
    "ax.set_ylabel('h, m')\n",
    "fig.suptitle(\"Track %s, Cycle %s, Beam 2L\" %(rgt,cycle),fontsize=16)\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "a3abbbfc65ef4e75a1edc253b91df2ae",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Canvas(toolbar=Toolbar(toolitems=[('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous …"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# plot ground track\n",
    "track_plot(D3['heights']['lat_ph'][::50],D3['heights']['lon_ph'][::50],float(rgt))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "# close figures\n",
    "plt.close(\"all\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
